#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <unordered_map>

using namespace std;

const string mydict = "./resource/dict.txt";
const string sep = " ";

class Translate
{
public:
    Translate(string dict_path = mydict)
    :_dict_path(dict_path)
    {
        LoadDict();
        Parse();
    }
    void LoadDict()
    {
        std::ifstream in(_dict_path);
        string line;
        while(getline(in, line))
        {
            lines.push_back(line);
        }
        in.close();
    }
    void Parse()
    {
        for(auto& line : lines)
        {
            //分割
            auto pos = line.find(sep);// XXX YYY
            if(pos == string::npos) continue;
            else
            {
                string word = line.substr(0, pos);
                string chinese = line.substr(pos+sep.size());
                _dict.insert(std::make_pair(word, chinese));
            }
        }
    }
    void debug()
    {
        //看是否加载成功
        for(auto& line : lines)
        {
            cout << line << endl;
        }
        for(auto& elem : _dict)
        {
            cout << elem.first << ":" << elem.second << endl;
        }
    }
    string Excute(const string &word)
    {
        auto iter = _dict.find(word);
        if (iter == _dict.end())
            return "unknown";
        else
            return _dict[word];
    }
    ~Translate() {}

private:
    std::string _dict_path;
    std::unordered_map<string, string> _dict;
    std::vector<string> lines;
};

//#define DEBUG
#ifdef DEBUG

int main()
{
    Translate trans;
    trans.debug();
}

#endif